package com.supreme.studyserver.utils;

import com.supreme.studyserver.entity.Student;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//@Component
public class StudentFactoryAware implements BeanFactoryAware, BeanFactoryPostProcessor, BeanNameAware, ApplicationContextAware {

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("setBeanFactory");
        final Student studentBean = beanFactory.getBean(Student.class);
        studentBean.getTeacher().setName("liu teacher");
        System.out.println(studentBean.getTeacher().getName());
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        System.out.println("postProcessBeanFactory");
        final Student studentBean = configurableListableBeanFactory.getBean(Student.class);
        System.out.println(studentBean.getTeacher().getName());
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("setApplicationContext");
        final Student studentBean = applicationContext.getBean(Student.class);
        System.out.println(studentBean.getTeacher().getName());
    }

    @Override
    public void setBeanName(String s) {
        System.out.println("setBeanName: " + s);
    }
}
